package com.edu.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.mail.MailUtil;
import com.edu.demo.entity.UserBean;
import com.edu.demo.entity.vo.RegisterByEmailVO;
import com.edu.demo.entity.vo.RegisterByPhoneVO;
import com.edu.demo.result.Result;
import com.edu.demo.service.IRegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.edu.demo.tool.MyTool.isPassWord;
import static com.edu.demo.tool.MyTool.isValidEmail;

/**
 * @author XYH
 * date 2024-03-08
 */

@Api(tags = "注册接口")
@RestController
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private IRegisterService registerService;

    RegisterByEmailVO registerByEmailVO = new RegisterByEmailVO();
    UserBean userBean = new UserBean();

    @ApiOperation(value = "获取验证码")
    @PostMapping("/getVerificationCode/{email}")
    public Result getVerificationCode(@PathVariable String email) {
        if ((System.currentTimeMillis() - registerByEmailVO.getGetCodeTime()) < 60000) {
            return Result.error(1002, "请一分钟后重试");
        }
        if (!isValidEmail(email)) {
            return Result.error(1000, "邮箱格式不正确");
        }
        int randomInt = RandomUtil.randomInt(100000, 999999);
        registerByEmailVO.setCode(Integer.toString(randomInt));
        registerByEmailVO.setEmail(email);
        registerByEmailVO.setGetCodeTime(System.currentTimeMillis());
        System.out.println("registerByEmailVO = " + registerByEmailVO);
        MailUtil.send(email, "xxx验证码", "xxx,用户您好!<br> 您正在验证邮箱，请填写以下验证码完成验证：<br><p>&nbsp;</p><strong><big>" + randomInt + "</big></strong><p>&nbsp;</p><br> 验证码在发送后3分钟内有效<hr>祝您生活愉快！", true);
        return Result.success("发送成功");
    }

    @ApiOperation(value = "邮箱注册")
    @PostMapping("/byEmail/{code}+{passWord}")
    public Result userRegisterByEmail(@PathVariable String code, @PathVariable String passWord) {
        if (!isPassWord(passWord)) {
            return Result.error(1000, "密码必须包含大小写和数字");
        }
        if ((System.currentTimeMillis() - registerByEmailVO.getGetCodeTime()) > (60000 * 3)) {
            return Result.error(1002, "验证码失效，请重新获取");
        }
        if (!registerByEmailVO.getCode().equals(code)) {
            return Result.error(1003, "验证码错误");
        }
        registerByEmailVO.setPassword(passWord);
        BeanUtil.copyProperties(registerByEmailVO, userBean);
        int results = registerService.registerByEmail(userBean);
        if (results != 1) {
            return Result.error(1004, "注册失败");
        }
        return Result.success("注册成功");
    }

    @ApiOperation(value = "手机号注册")
    @PostMapping("/byPhone")
    public Result userRegisterByPhone(@Valid @RequestBody RegisterByPhoneVO registerByPhoneVO) {
        System.out.println("registerByPhoneVO = " + registerByPhoneVO);
        BeanUtil.copyProperties(registerByPhoneVO,userBean);
        registerService.registerByPhone(userBean);
        return  Result.success().msg("用户注册成功");
    }
}
